<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Voucher_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_voucher';
	}
        
        
        public function getnegative($params,$sale_fare,$real_fare){
            if($sale_fare){
                $this->db->select('SUM(sale_fare) AS sale_fare');
                $this->db->where('sale_fare > 0');
            }
            if($real_fare){
                $this->db->select('SUM(real_fare) AS real_fare');
            }
            $this->db->from($this->table_name);
                
            if(isset($params['booking_place_code'])){
                $this->db->like('booking_place_code',$params['booking_place_code']);
            }
            if(isset($params['airline'])){
                $this->db->like('airline',$params['airline']);
            }
            if(isset($params['createdTime'])){
                $this->db->like('createdTime',$params['createdTime']);
            }
            
            $query = $this->db->get();
            //echo $this->db->last_query();exit;
            return $query->result();
        }

        public function updateSurplusAllCus($params){
                $data = array(
                    'residual' => 0
                 );
                 $this->db->where('customerID', $params['customerID']);
                 $this->db->where('owner', $params['owner']);
                 $this->db->update($this->table_name, $data);
        }

        public function getSurplusCusABooker($params){
                $this->db->select('sum(residual) as residual');
                $this->db->from($this->table_name);
                
                if(isset($params['customerID'])){
                    $this->db->where('customerID',$params['customerID']);
                }
                
                if(isset($params['owner'])){
                    $this->db->where('owner',$params['owner']);
                }
                $this->db->where('checkbox_sodukh',1);
                //checkbox_sodukh
                $query = $this->db->get();
		return $query->result();
        }

        public function getListBookingTypes(){
            return $this->booking_types;
        }
        
        public function checkVoucher($params){
                $this->db->select('*');
                $this->db->from($this->table_name);
                
                if(isset($params['airline'])){
                    $this->db->like('airline',$params['airline']);
                }
                
                if(isset($params['booking_place_code'])){
                    $this->db->like('booking_place_code',$params['booking_place_code']);
                }
                
                if(isset($params['createdTime'])){
                    $this->db->like('createdTime',$params['createdTime']);
                }
                
                $query = $this->db->get();
		return $query->result();
        }

        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '1%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'sumNet',
						'label' => $this->lang->line('sumNet'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'sumSale',
						'label' => $this->lang->line('sumSale'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'payment_method',
						'label' => $this->lang->line('payment_method'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'payment_company',
						'label' => $this->lang->line('payment_company'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'note_bk',
						'label' => $this->lang->line('note_bk'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name'  => 'button',
						'width' => '5%',
						'sort'  => FALSE,
						'label' =>  "",
						'searchoptions' => false
				)
		);
	}
	
	public function json_data($controller, $right){
            
                if($this->session->userdata('v_voucher')){
                    $arr_status = json_decode($this->session->userdata('v_voucher'));
                }
            
		$this->datatables
		->select("tbl.id,tbl.*,method.payment_name,bc.name,usr.username,bvd.payment_confirm AS approve,bvt.ticketWorkers")
                ->from($this->table_name.' AS tbl')
                ->join('book_payment_method AS method','method.id = tbl.payment_method')
                ->join('book_contacts AS bc','bc.customerID = tbl.customerID')
                ->join('users AS usr','usr.id=tbl.owner')
                ->join('book_voucher_debts AS bvd','bvd.voucher_id=tbl.id','LEFT')
                ->join('book_voucher_ticket AS bvt','bvt.voucher_id=tbl.id','LEFT')
                ->where('tbl.payment_confirm',0)->where('tbl.payment_method != 6');
                
                if(!in_array($this->session->userdata['group_id'],array(1,79))){
                    $this->datatables->where('tbl.owner',$this->session->userdata['user_id']);
                }
                if(in_array($this->session->userdata['user_id'],array(178))){
                    $this->datatables->where('tbl.payment_method',4);
                }
                
                if(isset($arr_status) && $arr_status->v_customerName != ''){
                    $this->datatables->where('bc.name REGEXP ("'.$arr_status->v_customerName.'")');
                }
                
                if(isset($arr_status) && $arr_status->v_booking_place_code != ''){
                    $this->datatables->where('tbl.booking_place_code LIKE "%'.$arr_status->v_booking_place_code.'%"');
                }
                
                if(isset($arr_status) && $arr_status->v_payment_method != ''){
                    $this->datatables->where('tbl.payment_method ',$arr_status->v_payment_method);
                }
                
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                
                $this->session->unset_userdata('v_voucher');
                
		foreach($datatables['aaData'] as $item){
                        $booking_place_code = json_decode($item['booking_place_code']);
                        $airline            = json_decode($item['airline']);
                        
                        $pnr                            = '';
                        foreach($booking_place_code as $key => $vals){
                            if($key < count($booking_place_code)-1){
                                $etc = ', ';
                            } else {
                                $etc = '';
                            }
                            $pnr .= $vals.'-'.$airline[$key].$etc;
                        }
                        
                        switch ($item['payment_method']){
                            case '2':
                                $info           =   $this->voucher_model->getNameTicker($item['id']);
                                if(empty($info)){
                                    $ticketer = '';
                                } else {
                                    $ticketer = $info[0]->name;
                                }
                                $payment_info   =   $item['payment_name'].'<br/>'.$ticketer;
                                break;
                            case '3':
                                $data           = $this->voucher_model->getInfoTable(array('voucher_id'=>$item['id']),'book_voucher_bank'); 
                                $_data          = $this->voucher_model->getInfoTable(array('bankid'=>$data[0]->bankid),'book_bank');
                                $payment_info   = $_data[0]->bank_name;
                                break;
                            case '4':
                                $status         = ($item['approve']==1)?$this->lang->line('approved'):$this->lang->line('unapproved');
                                $payment_info   = $item['payment_name'].'<br/>'.$status;
                                break;
                            case '5':
                                $payment_info   =   $item['payment_name'];
                                break;
                        }
			$ouput['aaData'][] = array(
					$item['id'],
                                        $item['name'],
                                        ($pnr)?$pnr:'',
                                        $this->bookinglib->my_number_format($item['net_fare'],2, ',', ','),
                                        $this->bookinglib->my_number_format($item['sale_fare'],2, ',', ','),
                                        $payment_info,
                                        ($item['payment_confirm'] == 1)?'Đã thanh toán':'Chưa thanh toán',
                                        $item['username'],
                                        $item['note_bk'],
                                        date("d-m-Y H:i:s",strtotime($item['lastupdated'])),
                                        $this->add_button_public($controller, $right, $item)
			);
		}
	
		return json_encode($ouput);
	}

}